package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.Orders;
import com.ruoyi.system.domain.PetSupply;
import com.ruoyi.system.domain.Vo.OrdersVo;
import com.ruoyi.system.service.IOrdersService;
import com.ruoyi.system.service.IPetSupplyService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/system/myorders")
public class MyOrdersController extends BaseController {
    private String prefix = "system/myorders";

    @Resource
    private IOrdersService ordersService;

    @Resource
    private IPetSupplyService petSupplyService;

    @Resource
    private ISysUserService userService;

    @GetMapping()
    public String reservation()
    {
        return prefix + "/orders";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Orders orders)
    {
        startPage();
        orders.setUserId(getSysUser().getUserId());
        List<Orders> list = ordersService.selectOrdersList(orders);
        List<OrdersVo> ordersVoList = new ArrayList<>();
        list.forEach(item->{
            PetSupply petSupply = petSupplyService.selectPetSupplyById(item.getSupplyId());
            OrdersVo build = OrdersVo.builder()
                    .amount(item.getAmount())
                    .customerId(item.getCustomerId())
                    .id(item.getId())
                    .supplyName(petSupply.getName())
                    .userName(userService.selectUserById(item.getUserId()).getUserName())
                    .imgUrl(petSupply.getImgUrl())
//                    金额除单价算数量
                    .number(item.getAmount().divide(petSupply.getPrice()).intValue())
                    .createTime(item.getCreateTime())
                    .updateTime(item.getUpdateTime())
                    .build();
            ordersVoList.add(build);
        });
        return getDataTable(ordersVoList);
    }
}
